/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.lis.ctrl;

import com.cxhd.lis.model.newmodel.LisReference;
import static com.cxhd.lis.model.newmodel.LisReference.lisReferencedao;
import com.cxhd.lis.model.newmodel.LisReferencevalue;
import static com.cxhd.lis.model.newmodel.LisReferencevalue.lisReferencevaluedao;
import com.cxhd.lis.model.newmodel.LisTestdescribe;
import static com.cxhd.lis.model.newmodel.LisTestdescribe.lisTestdescribedao;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import java.util.List;
import java.util.Map;

/**
 *
 * @author cxhd
 */
@ControllerBind(controllerKey = "/view/lis/lisTes")
public class LisTestdescribeCtrl extends Controller{
          Gson gson = new Gson();
     public void index(){
        render("/view/lis/ljxm.jsp");
    }
     public void getLisTes(){//项目查询
//         renderJson(Db.use("mysql").find("select t.PK,  t.TESTID,t.ENGLISHNAME,t.CHINESENAME,t.UNIT,t.LABDEPARTMENT,t.INUREDATE,t.PRICE,t.PRICE2 ,t.SAMPLETYPE, \n" +
//"(SELECT s.SAMPLEDESCRIBE  FROM lis_sampletype s WHERE s.SAMPLETYPE=t.SAMPLETYPE) SAMPLEDESCRIBE, \n" +
//"t.EMERGENCY,t.PRICE from lis_testdescribe t order by t.PK  limit 0,10"));
          String sql="select t.PK,t.TESTID,t.REFCLASS,t.ENGLISHNAME,t.SAMPLETYPE,t.CHINESENAME,t.UNIT,t.LABDEPARTMENT,t.INUREDATE,t.PRICE,t.PRICE2 ,t.SAMPLETYPE,\n"+
             "(SELECT s.SAMPLEDESCRIBE  FROM lis_sampletype s WHERE s.PK=t.SAMPLETYPE) SAMPLEDESCRIBE1,t.EMERGENCY,t.PRICE,(SELECT UNITNAME FROM lis_unit u where u.UNITNAME=t.UNIT ) UNIT1"     ;
     String param=" from lis_testdescribe t where t.TESTID like '%"+getPara("searchContent")+"%' or t.CHINESENAME like '%"+getPara("searchContent")+"%'";
Page<Record> userPage = Db.use("mysql").paginate(getParaToInt("currentPage",getParaToInt("currentPage")), getParaToInt("pageSize", getParaToInt("pageSize")), sql, param);
renderJson(userPage);
     }
     public void getLisTesReP(){
          List<Record> list= Db.use("mysql").find("select * from lis_testdescribe where CHINESENAME=?",getPara("CHINESENAME"));
       if(list.size()!=0){ 
         
            if(getPara("PK")!=""&&getPara("PK")!=null){//判断是新增还是修改
                if(Integer.valueOf(getPara("PK"))==list.get(0).get("PK")){ //修改是允许修改（本身主键和查询到的主键相同）
                     renderText("false");
                }else{
                   renderText("true");  
                }
            }else{
                renderText("true");
            }
        
        }else{
            renderText("false");
        }
     }
     public void getTestIdRep(){
         List<Record> list=null;
         if(getPara("PK")!=""&&getPara("PK")!=null){
              list=Db.use("mysql").find("select * from lis_testdescribe where TESTID=? or PK=?",getPara("TESTID"),getPara("PK"));
              if(list.size()==1){
                 renderText("false"); 
              }else{
                  renderText("true");
              }
         }else{
             list=Db.use("mysql").find("select * from lis_testdescribe where TESTID=?",getPara("TESTID"));
             if(list.size()==0){
              renderText("false"); 
              }else{
                  renderText("true");
              }
         }
//          List<Record> list= Db.use("mysql").find("select * from lis_testdescribe where TESTID=?",getPara("TESTID"));
//       if(list.size()!=0){ 
//            if(getPara("PK")!=""&&getPara("PK")!=null){//判断是新增还是修改
//                if(Integer.valueOf(getPara("PK"))==list.get(0).get("PK")){ //修改是允许修改（本身主键和查询到的主键相同）
//                     renderText("false");
//                }else{
//                   renderText("true");  
//                }
//            }else{
//                renderText("true");
//            }
//        }else{
//            renderText("false");
//        }
     }
     public void getLisRefe(){//项目参考范围查询
         
         renderJson(Db.use("mysql").find("select PK,SAMPLETYPE,TESTID,REFAGE,MREFLO0,MREFHI0,FREFLO0,FREFHI0,Decimalplace from  lis_referencevalue where TESTID=?",getPara("TESTID")));
     }
     @Before(Tx.class)
     public void deleteLisTes(){//先判断该项目是否被申请，操作等
         lisTestdescribedao.deleteTesd(getPara("TESTID"));
         lisReferencevaluedao.deleteLisRef(getPara("TESTID"));
         renderText("true");
     }
     @Before(Tx.class)
     public void saveLisTes(){
//         String testId=getTestId();
            Map<String, String> test = gson.fromJson(getPara("TEST"), new TypeToken<Map<String, String>>() {}.getType());
               List<Map<String, String>> teflist = gson.fromJson(getPara("TEF"), new TypeToken<List<Map<String, String>>>() {}.getType());
                List<Map<String, String>> refvallist = gson.fromJson(getPara("REFVALS"), new TypeToken<List<Map<String, String>>>() {}.getType());
       new LisTestdescribe().saveTesd(test.get("TESTID"),test.get("CHINESENAME"),test.get("ENGLISHNAME"),test.get("LABDEPARTMENT"), test.get("SAMPLETYPE"), test.get("UNIT"), test.get("EMERGENCY"), test.get("PRICE"),test.get("REFCLASS"));
       for(Map<String, String> tef :teflist){
       new LisReferencevalue().saveLisRef(test.get("TESTID"),  tef.get("REFAGE"),tef.get("MREFLO0"), tef.get("MREFHI0"),tef.get("FREFLO0"),tef.get("FREFHI0"),tef.get("SAMPLETYPE"),tef.get("Decimalplace"));
       }
        for(Map<String,String> tefval :refvallist){
          new LisReference().saveRefs(test.get("TESTID"), tefval.get("REFAGE"),tefval.get("SAMPLETYPEPK"), tefval.get("MREFVAL0"), tefval.get("FREFVAL0"), tefval.get("DECIMALPLACE"));
        }
       renderText("true");
     }
     @Before(Tx.class)
     public void updateTes(){
          Map<String, String> test = gson.fromJson(getPara("TEST"), new TypeToken<Map<String, String>>() {}.getType());
         List<Map<String, String>> teflist = gson.fromJson(getPara("TEF"), new TypeToken<List<Map<String, String>>>() {}.getType());
         List<Map<String, String>> refvallist = gson.fromJson(getPara("REFVALS"), new TypeToken<List<Map<String, String>>>() {}.getType());
         lisTestdescribedao.updateTest(test.get("PK"), test.get("CHINESENAME"), test.get("ENGLISHNAME"), test.get("LABDEPARTMENT"), test.get("SAMPLETYPE"), test.get("UNIT"),test.get("EMERGENCY"), test.get("PRICE"),test.get("REFCLASS"));
        //删除相关TESTID的所有参数范围 
        new LisReferencevalue().deleteLisRef(test.get("TESTID"));
        //把修改的值录入进表 
        for(Map<String, String> tef :teflist){
       new LisReferencevalue().saveLisRef(test.get("TESTID"),  tef.get("REFAGE"),tef.get("MREFLO0"), tef.get("MREFHI0"),tef.get("FREFLO0"),tef.get("FREFHI0"),tef.get("SAMPLETYPE"),tef.get("Decimalplace"));
       }
        new LisReference().deleteLisRerence(test.get("TESTID"));
        for(Map<String,String> tefval :refvallist){
          new LisReference().saveRefs(test.get("TESTID"), tefval.get("REFAGE"),tefval.get("SAMPLETYPEPK"), tefval.get("MREFVAL0"), tefval.get("FREFVAL0"), tefval.get("DECIMALPLACE"));
        }
//         lisReferencevaluedao.updateLisRef(tef.get("PK"), tef.get("REFAGE"), tef.get("MREFLO0"),tef.get("MREFHI0"), tef.get("FREFLO0"), tef.get("FREFHI0"),test.get("SAMPLETYPE"),tef.get("Decimalplace"));

         renderText("true");
     }
     
//      public String getTestId(){
//           List<Record> list=Db.use("mysql").find("select * from lis_testdescribe limit 0,10");
//        String  value = "1";
//        if(list.size()>0){
//           value = (Integer.parseInt(Db.use("mysql").findFirst("SELECT MAX(TESTID) as id from lis_testdescribe ").get("id") + "") + 1) + "";
//        }
//      return value;
//      }
     @Before(Tx.class)
       public void addRef(){//根据验证项目配置参数
           List<Record> list=Db.use("mysql").find("select * from lis_testdescribe");
           for(Record tmp : list){
               //
               lisReferencevaluedao.saveLisRef(tmp.getStr("TESTID"), "0", "0", "0", "0", "0","1","0");
           }
           renderText("true");
       }
       public void addPrindOrd(){//增加项目排序
           lisTestdescribedao.updatePrintOrd();
           renderText("true");
       }
       public void getUnit(){
           renderJson(Db.use("mysql").find("select PK,UNITNAME,UNITCHINA from lis_unit"));
       }
       public void getLisRerence(){//项目参考值查询
         
         renderJson(Db.use("mysql").find("select PK,TESTID,REFAGE,SAMPLETYPEPK,MREFVAL0,FREFVAL0,DECIMALPLACE from lis_reference where TESTID=?",getPara("TESTID")));
     }
       
       public void addLisRerences(){//根据验证项目配置参考值 刷新用
           List<Record> list=Db.use("mysql").find("select * from lis_testdescribe");
           for(Record tmp : list){
               //String ITEMPK,String REFAGE,String SAMPLETYPEPK,String MREFVAL0,String FREFVAL0,String DECIMALPLACE
               lisReferencedao.saveRefs(tmp.getStr("TESTID"), "0", "1", "0", "0", "0");                
           }
           renderText("true");
       }
}
